/*****************************************************************************
 *   timer.c:  Timer C file for NXP LPC23xx/24xx Family Microprocessors
 *
 *   Copyright(C) 2006, NXP Semiconductor
 *   All rights reserved.
 *
 *   History
 *   2006.09.01  ver 1.00    Prelimnary version, first Release
 *
******************************************************************************/
#include "LPC24xx.h"		/* LPC23xx/24xx Peripheral Registers	*/
#include "type.h"
#include "irq.h"
#include "timer.h"
#include "uart.h"
#include "motion.h"

extern int NOISE_stopped;
extern int STOP_THE_RECORDING;

volatile DWORD timer0_counter = 0;
volatile DWORD timer1_counter = 0;

/*****************************************************************************
** Function name:		delayMs
**
** Descriptions:		Start the timer delay in milo seconds
**						until elapsed
**
** parameters:			timer number, Delay value in milo second			 
** 						
** Returned value:		None
** 
*****************************************************************************/
void delayMs(BYTE timer_num, DWORD delayInMs)
{
  if ( timer_num == 0 )
  {
	/*
	* setup timer #0 for delay
	*/
	T0TCR = 0x02;		/* reset timer */
	T0PR  = 0x00;		/* set prescaler to zero */
	T0MR0 = delayInMs * (Fpclk / 1000-1);
	T0IR  = 0xff;		/* reset all interrrupts */
	T0MCR = 0x04;		/* stop timer on match */
	T0TCR = 0x01;		/* start timer */
  
	/* wait until delay time has elapsed */
	while (T0TCR & 0x01);
  }
  else if ( timer_num == 1 )
  {
	/*
	* setup timer #1 for delay
	*/
	T1TCR = 0x02;		/* reset timer */
	T1PR  = 0x00;		/* set prescaler to zero */
	T1MR0 = delayInMs * (Fpclk / 1000-1);
	T1IR  = 0xff;		/* reset all interrrupts */
	T1MCR = 0x04;		/* stop timer on match */
	T1TCR = 0x01;		/* start timer */
  
	/* wait until delay time has elapsed */
	while (T1TCR & 0x01);
  }
  return;
}

/******************************************************************************
** Function name:		Timer0Handler
**
** Descriptions:		Timer/Counter 0 interrupt handler
**						executes each 10ms @ 60 MHz CPU Clock
**
** parameters:			None
** Returned value:		None
** 
******************************************************************************/
DWORD light_on = 0;

void Timer0Handler (void)
{  
	DWORD currStatus;
	T0IR = 1;

	timer0_counter++;

	currStatus = AD0GDR;
	currStatus = (currStatus >> 6);
	currStatus &= (0x3FF);
	if (currStatus >= 0x00a) {
		//xprintf("Triggered at currStatus: %03x (timer0_counter = %d, NOISE_stopped = %d)\n", currStatus, timer0_counter, NOISE_stopped);
		//NOISE_stopped = 0;
	}
	else {
		//xprintf("0 triggered at currStatus: %03x (timer0_counter = %d, NOISE_stopped = %d)\n", currStatus, timer0_counter, NOISE_stopped);
		NOISE_stopped++;
	}


  if(timer0_counter == 500) {
	  //xprintf("\n\nAT 500: NOISE_stopped = %d\n", NOISE_stopped);

	  if(NOISE_stopped > 495) {
		  STOP_THE_RECORDING = 1;
	  }

	  NOISE_stopped = 0;
	  timer0_counter = 0;
  }

//  disable_interrupt();
  VICVectAddr = 0;	/* Acknowledge Interrupt */
}


/******************************************************************************
** Function name:		Timer1Handler
**
** Descriptions:		Timer/Counter 1 interrupt handler
**						executes each 10ms @ 60 MHz CPU Clock
**
** parameters:			None
** Returned value:		None
** 
******************************************************************************/
void Timer1Handler (void)
{  
  T1IR = 1;			/* clear interrupt flag */
  enable_interrupt();			/* handles nested interrupt */

  timer1_counter++;

  disable_interrupt();
  VICVectAddr = 0;	/* Acknowledge Interrupt */
}


/******************************************************************************
** Function name:		enable_timer
**
** Descriptions:		Enable timer
**
** parameters:			timer number: 0 or 1
** Returned value:		None
** 
******************************************************************************/
void enable_timer( BYTE timer_num )
{
  if ( timer_num == 0 )
  {
	T0TCR = 1;
  }
  else
  {
	T1TCR = 1;
  }
  return;
}

/******************************************************************************
** Function name:		disable_timer
**
** Descriptions:		Disable timer
**
** parameters:			timer number: 0 or 1
** Returned value:		None
** 
******************************************************************************/
void disable_timer( BYTE timer_num )
{
  if ( timer_num == 0 )
  {
	T0TCR = 0;
  }
  else
  {
	T1TCR = 0;
  }
  return;
}

/******************************************************************************
** Function name:		reset_timer
**
** Descriptions:		Reset timer
**
** parameters:			timer number: 0 or 1
** Returned value:		None
** 
******************************************************************************/
void reset_timer( BYTE timer_num )
{
  DWORD regVal;

  if ( timer_num == 0 )
  {
	regVal = T0TCR;
	regVal |= 0x02;
	T0TCR = regVal;
  }
  else
  {
	regVal = T1TCR;
	regVal |= 0x02;
	T1TCR = regVal;
  }
  return;
}

/******************************************************************************
** Function name:		init_timer
**
** Descriptions:		Initialize timer, set timer interval, reset timer,
**						install timer interrupt handler
**
** parameters:			timer number and timer interval
** Returned value:		true or false, if the interrupt handler can't be
**						installed, return false.
** 
******************************************************************************/
DWORD init_timer ( BYTE timer_num, DWORD TimerInterval ) 
{

	// Set up clock and stuff.
	PCONP |= (1 << 1); // enable TIM0
	PCONP |= (1 << 2); // enable TIM1

	/* Remark: already done on reset.
	PCLKSEL0 |= (0 << 1); // set P_CLK for TIM0 to /4    18MHz
	PCLKSEL0 |= (0 << 2); // set P_CLK for TIM1 to /4	 18MHz
	*/

  if ( timer_num == 0 )
  {
	timer0_counter = 0;
	T0MR0 = TimerInterval;
	T0MCR = 3;				/* Interrupt and Reset on MR0 */

	if ( install_irq( TIMER0_INT, (void *)Timer0Handler, HIGHEST_PRIORITY ) == FALSE )
	{
	  return (FALSE);
	}  
	else
	{
	  return (TRUE);
	}
  }
  else if ( timer_num == 1 )
  {
	timer1_counter = 0;
	T1MR0 = TimerInterval;
	T1MCR = 3;				/* Interrupt and Reset on MR1 */

	if ( install_irq( TIMER1_INT, (void *)Timer1Handler, HIGHEST_PRIORITY ) == FALSE )
	{
	  return (FALSE);
	}  
	else
	{
	  return (TRUE);
	}
  }
  return (FALSE);
}

/******************************************************************************
**                            End Of File
******************************************************************************/
